<?php
namespace app\sys\model;
use app\tools\upload\UploadQN;
use think\Validate;
use think\Db;
use app\base\model\Base;
class InfoM extends Base
{

       protected function _V($scene,$data)
       {
           $rule = [
               'dmname' => 'require',
               'title' => 'require',
               'keywords' => 'require',
               'description' => 'require',
               'email' => 'require|email',
               'qq' => 'require|number',
               'beian' => 'require',
               'address' => 'require',
               'comname' => 'require',
               'is_close' => 'require',
           ];
        $msg = [];
        $field = [
            'dmname'=>'域名',
            'title'=>'站点标题',
            'keywords'=>'关键字',
            'description'=>'描述',
            'email'=>'邮箱',
            'qq'=>'客服QQ',
            'beian'=>'备案信息',
            'address'=>'企业地址',
            'comname'=>'公司名称',
            'is_close'=>'是否关闭站点',
        ];
           $validate = new Validate($rule,$msg,$field);
           $validate->scene('info', ['dmname','title','keywords','description','email','qq','beian','address','comname','is_close']);
           $result = $validate->scene($scene)->check($data);
           if($result === false)
           {
               return $validate->getError();
           }
           else
           {
               return true;
           }

       }


    /**
     * 修改数据
     * @param $data
     * @return bool
     */
    public function info($data)
    {
        $res = $this->_V('info',$data);
        if($res !== true)
        {
            $this->code = -1;
            $this->msg = $res;
            return false;
        }
        $updateData=[];
        //处理图片
        $ret = uploadOne('logo', array(
            array(100, 100),
        ));
        if($ret['ok'] == 1)
        {
            $updateData['logo'] = toQiNiu($ret['images'][1]);   // 第一个缩略图地址
        }
        else if($ret['ok'] == 2)
        {
        }
        else
        {
            $this->code = -1;
            $this->msg = $ret['error'];
            return false;
        }


        $updateData['dmname'] = $data['dmname'];
        $updateData['title'] = $data['title'];
        $updateData['keywords'] = $data['keywords'];
        $updateData['address'] = $data['address'];
        $updateData['description'] = $data['description'];
        $updateData['email'] = $data['email'];
        $updateData['linkman'] = $data['linkman'];
        $updateData['tel'] = $data['tel'];
        $updateData['fax'] = $data['fax'];
        $updateData['qq'] = $data['qq'];
        $updateData['beian'] = $data['beian'];
        $updateData['comname'] = $data['comname'];
        $updateData['is_close'] = $data['is_close'];
        $updateData['admin_id'] = session('admin_id');
        $updateData['updated_at'] = time();
        $res = Db::table('sys_info')->where('id',$data['id'])->update($updateData);
        if(!$res)
        {
            $this->code = -1;
            $this->msg = '修改失败';
            return false;
        }
        return true;
    }

    /**
     * 删除数据
     * @param $id
     */
    public function del($id)
    {
        $res = Db::table('shop_shop')->where('id',$id)->field('is_usable')->find();
        $res['is_usable'] == 1?$zt = 0:$zt = 1;
        return Db::table('shop_shop')->where('id',$id)->update(['is_usable'=>$zt]);
    }
}
